﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Neits.UserService;
using Neits.Common;
using System.Security.Principal;

namespace Projecturo.Model
{
    public class User : IUser, IPrincipal, IChanged, ICreated
    {
        #region IUser Member

        public virtual IDataContext DataContext
        {
            get;
            set;
        }

        public virtual string FirstName
        {
            get;
            set;
        }

        public virtual bool IsActive
        {
            get;
            set;
        }

        public virtual string LastName
        {
            get;
            set;
        }

        public virtual string Password
        {
            get;
            set;
        }

        public virtual string ShortName
        {
            get;
            set;
        }

        public virtual string Username
        {
            get;
            set;
        }

        public virtual UserRoles Role
        {
            get;
            set;
        }

        public virtual DateTime CreateDate { get; set; }

        public virtual DateTime ChangeDate { get; set; }

        public virtual IUser CreatedBy { get; set; }

        public virtual IUser ChangedBy { get; set; }

        public virtual IList<Project> Projects { get; set; }

        #endregion

        #region IInstanceIdentifier Member

        public virtual long Id
        {
            get;
            set;
        }

        #endregion

        #region IPrincipal Member

        public virtual IIdentity Identity
        {
            get 
            {
                UserIdentity identity = new UserIdentity();
                identity.Name = this.ShortName;
                identity.IsAuthenticated = Id > 0;
                return identity;
            }
        }

        public virtual bool IsInRole(string role)
        {
            // TODO implement roles
            return true;
        }

        #endregion
    }

    public enum UserRoles
    {
        Unknown = 0,
        Administrator = 1,
        Contributor = 2,
        Reader = 3,
    }

    public class UserIdentity : IIdentity
    {
        private bool isAuthenticated = false;
        private string name;

        #region IIdentity Member

        public string AuthenticationType
        {
            get { return "Standard"; }
        }

        public bool IsAuthenticated
        {
            get { return isAuthenticated; }
            internal set { isAuthenticated = value; }
        }

        public string Name
        {
            get { return name; }
            internal set { name = value; }
        }

        #endregion
    }
}
